package com.cskaoyan.controller.admin.role;

import com.cskaoyan.anno.ValidAnno;
import com.cskaoyan.bean.MarketRole;
import com.cskaoyan.bean.bo.admin.role.AdminRolePermissionBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.admin.role.AdminRoleVo;
import com.cskaoyan.bean.vo.admin.role.permission.AdminPermissionVo;
import com.cskaoyan.service.admin.role.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author MoJunXiang
 * @since 2022/09/08 17:05
 */
@RestController
@RequestMapping("admin/role")
public class AdminRoleController {
    @Autowired
    AdminRoleService adminRoleService;


    /**
     * @function_name list
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param name
     * @idea 返回所有 **角色** 的数据
     * @return com.cskaoyan.bean.common.BaseRespVo<com.cskaoyan.bean.vo.admin.role.AdminRoleVo>
     * @author MoJunXiang
     * @since 2022/09/08 19:45
     */
    @RequestMapping("list")
    public BaseRespVo<AdminRoleVo> list(Integer page, Integer limit, String sort, String order, String name) {
        AdminRoleVo roleVo = adminRoleService.list(page, limit, sort, order, name);

        // 返回值
        BaseRespVo<AdminRoleVo> vo = new BaseRespVo<>();
        vo.setErrmsg("成功");
        vo.setErrno(0);
        vo.setData(roleVo);

        return vo;
    }


    /**
     * @function_name options
     * @idea 返回 **market_role表** 中需要的所有 **角色** 的 *id* 和 *name*
     * @return com.cskaoyan.bean.common.BaseRespVo<com.cskaoyan.bean.vo.admin.role.AdminRoleVo>
     * @author MoJunXiang
     * @since 2022/09/08 20:13
     */
    @RequestMapping("options")
    public BaseRespVo<AdminRoleVo> options() {
        AdminRoleVo roleVo = adminRoleService.options();


        // 返回值
        BaseRespVo<AdminRoleVo> vo = new BaseRespVo<>();
        vo.setErrmsg("成功");
        vo.setErrno(0);
        vo.setData(roleVo);

        return vo;
    }


    /**
     * @function_name create
     * @param paraMap
     * @idea 获得请求中包含的信息 *name* 和 *desc* ，将其放入新的 **MarketRole对象role** 中，并且为其设置 *add_time* 、 *update_time*  ，将 **role** 插入数据库，并从数据库获得其  *id*  。将数据完整的 **role** 返回给前端。
     * @return com.cskaoyan.bean.common.BaseRespVo<com.cskaoyan.bean.MarketRole>
     * @author MoJunXiang
     * @since 2022/09/08 21:22
     */
    // @RequestMapping(value="create" , method = {RequestMethod.POST})
    @PostMapping("create")
    public BaseRespVo<MarketRole> create(@RequestBody Map<String, String> paraMap) {

        MarketRole roleVo = adminRoleService.create(paraMap);

        // 返回值
        BaseRespVo<MarketRole> vo = new BaseRespVo<>();
        vo.setErrmsg("成功");
        vo.setErrno(0);
        vo.setData(roleVo);

        return vo;

    }



    /**
     * @function_name update
     * @param role
     * @idea 将在管理系统中改变的 **role** 数据更新到数据库中的 **market_role** 表中
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author MoJunXiang
     * @since 2022/09/09 14:02
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketRole role) {
        adminRoleService.update(role);

        return BaseRespVo.ok(null);
    }



    /**
     * @function_name delete
     * @param role
     * @idea将指定 **角色（管理员）** 的信息传入， *逻辑删除（即令其 deleted=true）* 指定 *id* 的 **角色（管理员）**
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author MoJunXiang
     * @since 2022/09/09 14:23
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketRole role) {
        adminRoleService.delete(role);

        return BaseRespVo.ok(null);
    }


    @GetMapping("permissions")
    public BaseRespVo<AdminPermissionVo> permissions(Integer roleId) {
        AdminPermissionVo permissionVo = adminRoleService.permissions(roleId);

        return BaseRespVo.ok(permissionVo);
    }

    /**
     * @function_name permissions（POST）
     * @param bo
     * @idea 将传来的数据放入 **market_role_permission表**
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author MoJunXiang
     * @since 2022/09/09 17:39
     */
    @PostMapping("permissions")
    @ValidAnno
    public BaseRespVo permissions(@RequestBody @Valid AdminRolePermissionBo bo, BindingResult bindingResult) {
        adminRoleService.permissions(bo);


        return BaseRespVo.ok(null);
    }

}
